#define PURPLE_PLUGINS

#include "debug.h"
#include "plugin.h"
#include "version.h"

#include "gtkconv.h"
#include "gtkplugin.h"

static gchar *
text_replace_non_bmp (const gchar *utf8_input, glong input_length, glong *output_length)
{
	gunichar *ucs4_text;
	gunichar suspect;
	gchar *utf8_text;
	glong ucs4_length;
	glong index;
	
	ucs4_text = g_utf8_to_ucs4_fast (utf8_input, input_length, &ucs4_length);
	//ucs4_text = g_utf8_to_ucs4(utf8_input, input_length, NULL, &ucs4_length, NULL);
	
	for (index = 0; index < ucs4_length; index++)
	{
		suspect = ucs4_text[index];
		if ((suspect >= 0x1D173 && suspect <= 0x1D17A)
			|| (suspect >= 0xE0001 && suspect <= 0xE007F))
		{
			ucs4_text[index] = 0xFFFD; /* replacement character */
		}
	}
	
	utf8_text = g_ucs4_to_utf8 (
			ucs4_text,
			ucs4_length,
			NULL,
			output_length,
			NULL
	);
	g_free (ucs4_text);
	
	return utf8_text;
}

static gboolean
writing_im_msg_cb(PurpleAccount *account, const gchar *who,
					gchar **message, PurpleConversation *conv,
					PurpleMessageFlags flags)
{
	gchar *old_message = *message;
	gchar *new_message = text_replace_non_bmp(old_message, -1, NULL);
	if (new_message != NULL)
	{
		g_free(old_message);
		*message = new_message;
	}
	
	return FALSE;
}

static gboolean
writing_chat_msg_cb(PurpleAccount *account, const gchar *who,
					gchar **message, PurpleConversation *conv,
					PurpleMessageFlags flags)
{
	gchar *old_message = *message;
	gchar *new_message = text_replace_non_bmp(*message, -1, NULL);
	if (new_message != NULL)
	{
		g_free(old_message);
		*message = new_message;
	}
	
	return FALSE;
}

static gboolean
plugin_load(PurplePlugin *plugin)
{
	void *conv_handle = purple_conversations_get_handle();
	
	purple_signal_connect(conv_handle, "writing-chat-msg",
						plugin, PURPLE_CALLBACK(writing_chat_msg_cb), NULL);
	purple_signal_connect(conv_handle, "writing-im-msg",
						plugin, PURPLE_CALLBACK(writing_im_msg_cb), NULL);

	return TRUE;
}

static gboolean
plugin_unload(PurplePlugin *plugin)
{
	return TRUE;
}

static PurplePluginInfo info =
{
	PURPLE_PLUGIN_MAGIC,
	PURPLE_MAJOR_VERSION,
	0,
	PURPLE_PLUGIN_STANDARD,                             /**< type           */
	PIDGIN_PLUGIN_TYPE,                                             /**< ui_requirement */
	0,                                                /**< flags          */
	NULL,                                             /**< dependencies   */
	PURPLE_PRIORITY_DEFAULT,                            /**< priority       */

	"gtk-nonbmp-filter",                                 /**< id             */
	"Win32 Non-BMP Filter",                              /**< name           */
	"0.1",                                  /**< version        */
	                                                  /**  summary        */
	"Filters crashy characters from conversations.",
	                                                  /**  description    */
	"Filters crashy characters from conversations.",
	"Eion Robb <eionrobb@gmail.com>",        /**< author         */
	"",                                     /**< homepage       */

	plugin_load,                                      /**< load           */
	plugin_unload,                                    /**< unload         */
	NULL,                                             /**< destroy        */

	NULL,                                             /**< ui_info        */
	NULL,                                             /**< extra_info     */
	NULL,
	NULL,
	/* Padding */
	NULL,
	NULL,
	NULL,
	NULL
};

static void
init_plugin(PurplePlugin *plugin)
{
}

#ifdef _WIN32
PURPLE_INIT_PLUGIN(nonbmp_filter, init_plugin, info)
#endif
